import DataItemModel from "../../../cfw/mvc/DataItemModel";
import { StateItemModelEnum } from "../../../public/GameEnumConfig";
import User from "../../db/User";
import { MAX_STATE_NUM } from "../../../public/Config";
/**
* 宠物状态
**/
export default class StateItemModel extends DataItemModel {

	static CLASS_NAME: string = 'StateItemModel'

	private curValue: number = 0;

	private subStep: number = 0;
	init(id, data) {
		super.init(id, data)
		this.curValue = User.instance().getPlayerStateValue(this.ID)
		this.subStep = this.getSubnum() / 60;
	}

	setState(curValue: number) {
		this.curValue = curValue;
		User.instance().setPlayerStateValue(this.ID, curValue)
	}

	addNum(num: number) {
		this.curValue += num
		// cc.log('StateItemModel addNum  curValue == ', this.curValue, ' num ', num, ' id ===  ', this.ID)
		if (this.curValue > MAX_STATE_NUM) {
			this.curValue = MAX_STATE_NUM;
		}
		this.setState(this.curValue)
	}

	updateNum() {
		if (this.curValue > 0) {
			this.curValue -= this.subStep
			if (this.curValue < 0) {
				this.curValue = 0;
			}
			this.setState(this.curValue)
		}

	}

	getCurValue() {
		return this.curValue;
	}

	getPercent() {
		return this.curValue / MAX_STATE_NUM;
	}

	// 状态类别
	getType() {
		return this.data[StateItemModelEnum.type]
	}
	// 每分钟递减值
	getSubnum() {
		return this.data[StateItemModelEnum.subnum]
	}


	getExp() {
		return this.data[StateItemModelEnum.exp]
	}


}